<?php

/**
 * 获取登陆用户ID
 * @param string $str
 * @return number
 * @author Ice <iceinto@163.com>
 */
function getLoginUid($str = '')
{
    return intval($_SESSION[C('USER_AUTH_KEY') . $str]);
}

/**
 * 设置登陆用户ID
 * @param int $uid
 * @param string $str
 * @author Ice <iceinto@163.com>
 */
function setLoginUid($uid, $str = '')
{
    $_SESSION[C('USER_AUTH_KEY') . $str] = $uid;
}

/**
 * 时间格式化
 * @param $time
 * @param string $fromat
 * @return bool|string
 */
function timeToDate($time, $fromat = 'Y-m-d')
{
    if (!empty($time)) {
        return date($fromat, $time);
    }
}

/**
 * 获取基础的URL
 * @return string
 */
function getBaseUrl()
{
    //return $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://' . $_SERVER['HTTP_HOST'] . (strpos($_SERVER['HTTP_HOST'], ':') === false ? $_SERVER['SERVER_PORT'] == '80' ? '' : ':' . $_SERVER['SERVER_PORT'] : '');
    return $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://' . $_SERVER['HTTP_HOST'];
}

/**
 * 状态说明
 * @param $status
 * @return mixed
 */
function statusInfo($status)
{
    $statusInfo = array();
    $statusInfo[0] = '不可用';
    $statusInfo[1] = '可用';
    $statusInfo[99] = '作废';
    $statusInfo[99] = '过期';
    $statusInfo[100] = '完成';
    return $statusInfo[$status];
}

/**
 * 格式化字节大小
 * @param  number $size 字节数
 * @param  string $delimiter 数字和单位分隔符
 * @return string            格式化后的带单位的大小
 * @author ice <iceinto@163.com>
 */
function format_bytes($size, $delimiter = '')
{
    $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
    for ($i = 0; $size >= 1024 && $i < 5; $i++) $size /= 1024;
    return round($size, 2) . $delimiter . $units[$i];
}

/**
 * Logic 简单调用
 * @param $name 名称
 * @return mixed
 */
function LD($name)
{
    return D($name, 'Logic');
}

/**
 * Service 简单调用
 * @param $name
 * @return mixed
 */
function SD($name)
{
    return D($name, 'Service');
}

/**
 * 把对象转换成数组
 * @param   object $object 要转换的对象
 * @return  array
 */
function objectToArray($object)
{
    if (count($object) == 0) return trim((string)$object);
    $result = array();
    $object = is_object($object) ? get_object_vars($object) : $object;
    foreach ($object as $key => $val) {
        $val = (is_object($val) || is_array($val)) ? objectToArray($val) : $val;
        $result[$key] = $val;
    }
    return $result;
}